package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.dao.CheckGroupMapper;
import com.xrit.sunnyday.pojo.CheckGroup;
import com.xrit.sunnyday.service.CheckGroupService;
import com.xrit.sunnyday.common.MessageConstant;
import com.xrit.sunnyday.common.PageResult;
import com.xrit.sunnyday.common.QueryPageBean;
import com.xrit.sunnyday.common.SunnydayException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
@Service(interfaceClass= CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    @Override
    public PageResult findPageAll(QueryPageBean queryPageBean) {
        //判断是否有查询条件
        if(!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }
        //开启分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //调用查询方法
        List<CheckGroup> checkGroupList = checkGroupMapper.findPageAll(queryPageBean.getQueryString());
        //将结果放置pageinfo中
        PageInfo pageInfo = new PageInfo(checkGroupList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
        //先添加检查组
        checkGroupMapper.add(checkGroup);
        //获得检查组的id
        Integer groupId = checkGroup.getId();
        System.out.println(groupId);
        //循环遍历checkitems
        for (Integer checkitemId : checkitemIds) {
            //添加到检查组检查项中间表
            checkGroupMapper.addCheckGroupCheckItem(groupId,checkitemId);
        }
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }

    @Override
    public List<Integer> findCheckGroupItem(Integer id) {
        return checkGroupMapper.findCheckGroupItem(id);
    }

    @Override
    public void deleteById(Integer id) throws SunnydayException {
        //1,查询询检查组和套餐的中间表,判断该检查组和套餐是否有关系
        int countMenu = checkGroupMapper.findCheckGroupMenu(id);
        //2,如果有关系不能删除该检查组
        if(countMenu>0){
            throw new SunnydayException(MessageConstant.DELETE_CHECKGROUP_ERROR);
        }
        //3,查询检查组和检查项的中间表,判断该检查组和检查项是否有关系
        int counrgroup = checkGroupMapper.findCheckGroupItemById(id);
        //4,如果有关系删除检查组和检查项的中间表(解除关系)
        if(counrgroup>0){
            checkGroupMapper.deleteCheckGroupItemById(id);
        }
        //5,删除检查组
        checkGroupMapper.deleteById(id);
    }
    @Override
    public void update(Integer[] ids, CheckGroup checkGroup) {
        //根据id修改检查组
        checkGroupMapper.update(checkGroup);
        //根据检查组id删除对应的检查项
        checkGroupMapper.deleteCheckGroupItem(checkGroup.getId());
        //在将检查组和检查项的id重新添加到中间表
        for (Integer id : ids) {
            checkGroupMapper.addCheckGroupCheckItem(checkGroup.getId(),id);
        }
    }
}
